package com.example.administrator.base.baseActivity;


import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.administrator.base.activity.Login;
import com.example.administrator.base.activity.MainActivity;
import com.example.administrator.base.bean.ReLogin;
import com.example.administrator.base.util.AppManager;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SharedPreferencesUtils;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;


public abstract class BaseActivity extends AppCompatActivity {



    protected final String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        AppManager.getAppManager().addActivity(this);
        Log.d(TAG, "BaseActivity-->onCreate()");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart()");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        Log.d(TAG, "onResume()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        Log.d(TAG, "onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop()");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
        Log.d(TAG, "onDestroy()");
    }
    /**
     * [简化Toast]
     */
    protected void showToast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    /*网络请求*/
    protected void urlRequest(Request request, final String action){
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //传入Request
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (!isFinishing()){
                    onResponseFailure(e,action);
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
            if (!isFinishing()){
                onResponseSuccess(response,action);
            }
            }
        });
    }

    protected void postRequest(final Request request, final String action){
        final OkHttpClient client = new OkHttpClient();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        if (!isFinishing()){
                            onResponseSuccess(response,action);
                        }
                    } else {

                        throw new IOException("Unexpected code " + response);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (!isFinishing()){
                        onResponseFailure(e,action);
                    }
                }
            }
        }).start();
    }



    protected abstract void onResponseSuccess(Response response,String action);

    protected abstract void onResponseFailure(Throwable e,String action);




}